<?php
namespace Rainrock\Framework\Model;


use Rainrock\Framework\kernel\core\Model;
use Rainrock\Framework\kernel\base\Rock;

class optionModel extends Model{
	
	
	/**
	*	设置选项
	*/
	public function setval($num, $val='', $name='')
	{
		$numa	= explode('@', $num);
		$num 	= $numa[0];
		$where  = "`num`='$num'";
		$ors 	= $this->getone($where);
		$id 	= 0;
		if($ors)$id 	= (int)$ors['id'];
		$arr 	= array(
			'num'	=> $num,
			'value'	=> $val,
			'valid'	=> 1,
			'optdt'	=> Rock::now()
		);
		if(isset($numa[1]))$arr['pid'] 	= $numa[1];
		if($name)$arr['name'] 	= $name;
		if($id==0){
			$this->insert($arr);
		}else{
			$this->update($arr, $where);
		}
		if($id==0)$id = $this->nDb->insert_id();
		return $id;
	}
	
	
	/**
	*	读取选项
	*/
	public function getval($num, $dev='', $lx=0)
	{
		$val= '';
		$rs = $this->getone("`num`='$num'", 'name,value,id,optdt');
		if($rs){
			if($lx==0)$val=$rs['value'];
			if($lx==1)$val=$rs['name'];
			if($lx==2)$val=$rs['id'];
			if($lx==3)$val=$rs['optdt'];
		}
		if($this->isempt($val))$val=$dev;
		return $val;
	}
}